package com.cn.ant.modules.wechat.web;

import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.DateUtils;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.sys.entity.User;
import com.cn.ant.modules.sys.utils.UserUtils;
import com.cn.ant.modules.wechat.entity.AutoResponse;
import com.cn.ant.modules.wechat.entity.TextTemplate;
import com.cn.ant.modules.wechat.entity.WeixinAccount;
import com.cn.ant.modules.wechat.service.AutoResponseService;
import com.cn.ant.modules.wechat.service.TextTemplateService;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 关键词管理Controller
 * @author AntDream
 * @version 2015-03-24
 */
@Controller
@RequestMapping(value = "${adminPath}/wechat/autoResponse")
public class AutoResponseController extends BaseController {

	@Autowired
	private AutoResponseService autoResponseService;
    @Autowired
    private TextTemplateService textTemplateService;
    @Autowired
    private WeixinAccountServiceImpl accountService;
	
	@ModelAttribute
	public AutoResponse get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return autoResponseService.get(id);
		}else{
			return new AutoResponse();
		}
	}

    @RequestMapping(value = "find", method = RequestMethod.POST)
    @ResponseBody
    public AutoResponse find(@RequestParam(required = true) String id) {
        return autoResponseService.get(id);
    }

    @RequiresPermissions("wechat:autoResponse:view")
	@RequestMapping(value = {"list", ""})
	public String list(AutoResponse autoResponse, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
        Map<String,Object> param = new HashMap<String,Object>();
        List<AutoResponse> list = autoResponseService.query(param);
        List<TextTemplate> templateList =  textTemplateService.queryForList(param);
        List<WeixinAccount> accountList = accountService.findByLoginUser();
        model.addAttribute("list", list);
        model.addAttribute("templateList", templateList);
        model.addAttribute("accountList", accountList);
		return "modules/wechat/autoResponseList";
	}

	@RequiresPermissions("wechat:autoResponse:view")
	@RequestMapping(value = "form")
	public String form(AutoResponse autoResponse, Model model) {
		model.addAttribute("autoResponse", autoResponse);
		return "modules/wechat/autoResponseForm";
	}

	@RequiresPermissions("wechat:autoResponse:view")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(AutoResponse autoResponse, Model model) {
        AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, autoResponse)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
			} else {
				if (StringUtils.isBlank(autoResponse.getId())) {
					autoResponse.setId(Identities.generateUUID());
                    autoResponse.setAddtime(DateUtils.getDate());
                    autoResponseService.save(autoResponse);
				} else {
					autoResponseService.update(autoResponse);
				}
			}
			msg = "保存关键词管理'" + autoResponse.getTemplatename() + "'成功";
            jo.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("wechat:autoResponse:view")
	@RequestMapping(value = "delete")
    @ResponseBody
	public AjaxJson delete(String id, RedirectAttributes redirectAttributes) {
        AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(id)) {
            autoResponseService.delete(id);
            jo.setMsg("删除公众号成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}
